package org.robot.scm.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @Author : Awn create on 2023/11/2 14:14
 */
public class DateTool {

    public static final String PATTERN_1 = "yyyy-MM-dd HH:mm:ss";
    public static final String PATTERN_2 = "yyyy-MM-dd HH:mm";
    public static final String PATTERN_3 = "HH:mm:ss";
    public static final String PATTERN_4 = "HH:mm";
    public static final String PATTERN_5 = "yyyy-MM-dd";
    public static final String PATTERN_6 = "yyyy-MM-dd HH:00:00";
    public static final String PATTERN_7 = "MM-dd HH:mm";
    public static final String PATTERN_8 = "HH:mm:ss.SSS";

    public static Date parse(String date) {
        try {
            return new SimpleDateFormat(PATTERN_1).parse(date);
        } catch (Exception e) {
            return new Date();
        }
    }

    public static Date parse(String date, String pattern) {
        try {
            return new SimpleDateFormat(pattern).parse(date);
        } catch (Exception e) {
            return new Date();
        }
    }

    public static String format(Long millis, String pattern) {
        return new SimpleDateFormat(pattern).format(new Date(millis));
    }

    public static String format(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }

    /**
     * 给定毫秒值，返回时分秒毫秒
     */
    public static String millToHmsMs(long millis) {
        long hours = TimeUnit.MILLISECONDS.toHours(millis);
        long remainderMillis = millis - (hours * TimeUnit.HOURS.toMillis(1));
        long minutes = TimeUnit.MILLISECONDS.toMinutes(remainderMillis);
        remainderMillis -= (minutes * TimeUnit.MINUTES.toMillis(1));
        long seconds = TimeUnit.MILLISECONDS.toSeconds(remainderMillis);
        // 裁剪多余的毫秒
        remainderMillis -= (seconds * TimeUnit.SECONDS.toMillis(1));
        // 如果有毫秒，保留到三位
        int formattedRemainderMillis = (int) Math.round(remainderMillis / 100.0 * 1000);
        return String.format("%d时%d分%d秒%.3f毫秒", hours, minutes, seconds, formattedRemainderMillis / 1000.0);
    }

    /**
     * 给定毫秒值，返回时分秒
     */
    public static String millToHms(long millis) {
        long hours = TimeUnit.MILLISECONDS.toHours(millis);
        long remainderMillis = millis - (hours * TimeUnit.HOURS.toMillis(1));
        long minutes = TimeUnit.MILLISECONDS.toMinutes(remainderMillis);
        remainderMillis -= (minutes * TimeUnit.MINUTES.toMillis(1));
        long seconds = TimeUnit.MILLISECONDS.toSeconds(remainderMillis);
        return String.format("%d时%d分%d秒", hours, minutes, seconds);
    }

    /**
     * 给定毫秒值，返回分秒
     *
     * @param millis
     * @return
     */
    public static String millToms(long millis) {
        long hours = TimeUnit.MILLISECONDS.toHours(millis);
        long remainderMillis = millis - (hours * TimeUnit.HOURS.toMillis(1));
        long minutes = TimeUnit.MILLISECONDS.toMinutes(remainderMillis);
        remainderMillis -= (minutes * TimeUnit.MINUTES.toMillis(1));
        long seconds = TimeUnit.MILLISECONDS.toSeconds(remainderMillis);
        return String.format("%d分%d秒", minutes, seconds);
    }

    public static String format(Date date) {
        return format(date, PATTERN_1);
    }

    public static String currentDate() {
        return format(new Date(), PATTERN_5);
    }

    public static Date current() {
        return new Date();
    }

    public static Date current(int subDay) {
        return new Date(System.currentTimeMillis() + TimeUnit.DAYS.toMillis(subDay));
    }
}
